package com.ruoyi.common.convert;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;

/**
 * 布尔类型转为字符串
 */
public class BoolToStringDeserializer extends JsonDeserializer<String> {
    @Override
    public String deserialize(JsonParser p, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        JsonToken t = p.currentToken();
        if (t == JsonToken.VALUE_TRUE) {
            return "1";
        } else if (t == JsonToken.VALUE_FALSE) {
            return "0";
        } else {
            String res = p.getCodec().readTree(p).toString();

            String result = res.replace("\"", "");
            System.out.println("布尔类型转为字符串==> " + result);
            return result;
        }
    }
}
